Test Failed
Pull Request — master (#2)
by Yo
01:40
created

index.js ➔ ???   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 3
c 5
b 0
f 0
nc 2
nop 2
dl 0
loc 10
rs 9.4285
1
"use strict";
2
3
if (['dev', 'prod'].indexOf(process.env.NODE_ENV)) {
4
    console.warn(`Undefined node environment "${process.env.NODE_ENV}", fallback to dev !`);
5
    process.env.NODE_ENV = 'dev';
6
}
7
8
const ON_DEATH = require('death')({
9
    SIGTERM: false,
10
    SIGQUIT: false
11
});
12
13
const logger = require('./lib/logger');
14
const taskLoggerFactory = require('./lib/logger/taskLogger');
15
const server = require('./lib/server');
16
const pkg = require('./package.json');
17
const NestedError = require('nested-error-stacks');
18
19
const appName = pkg.name;
20
const taskLogger = taskLoggerFactory(appName);
21
22
ON_DEATH((signal, err) => {
0 ignored issues
show
Unused Code introduced by
The parameter err is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
23
    let exitCode = 0;
24
    if (signal === 'SIGTERM' || signal === 'SIGQUIT') {
25
        exitCode = 1;
26
    }
27
28
    console.log('ON_DEATH');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
29
30
    cleanAndExit(exitCode);
31
});
32
33
process.on('unhandledRejection', (reason, p) => {
34
    console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
35
36
    return cleanAndExit(1);
37
});
38
39
40
const cleanAndExit = (exitCode = 0) => {
41
    taskLogger.stopping();
42
    const _exit = error => {
43
        if (error instanceof Error) {
44
            console.error(error);
45
        }
46
        taskLogger.stopped();
47
        process.exit(exitCode);
1 ignored issue
show
Compatibility Debugging Code Best Practice introduced by
Use of process.exit() is discouraged as it will potentially stop the complete node.js application. Consider quitting gracefully instead by throwing an Error.
Loading history...
48
    };
49
50
    return server.stop()
51
        .then(_exit, _exit);
52
};
53
54
taskLogger.starting();
55
56
return server.start()
57
    .then(() => taskLogger.started())
58
    .catch(error => {
59
        const newError = new NestedError(`Exit ${appName} after an error at initialisation`, error);
0 ignored issues
show
Unused Code introduced by
The constant newError seems to be never used. Consider removing it.
Loading history...
60
        logger.error(error.stack);
61
62
        return Promise.reject(cleanAndExit(1));
63
    });
64